package com.bicon.botu.mall.base.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import com.bicon.botu.mall.base.common.ValidationUtil;
import com.bicon.botu.mall.base.dto.DictionaryDTO;
import com.bicon.botu.mall.base.entity.Dictionary;
import com.bicon.botu.mall.base.entity.LogisticsCompany;
import com.bicon.botu.mall.base.entity.MallConfig;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商城系统设置控制器
 * </p>
 *
 * @author tandk
 * @since 2017-07-31
 */
@RestController
@RequestMapping("/V3.0.0/api/system-config")
public class BaseConfigAPI extends BaseController {
    /**
     * 根据父code获取子字典
     */
    @RequestMapping(value = "/findChildDictionary")
    public BaseResult findChildDictionary(@RequestParam String code) {
        if(ValidationUtil.isEmpty(code)){
            return sendResult400("参数不完整");
        }
        Dictionary dictionary = new Dictionary().selectOne(new EntityWrapper<Dictionary>().eq("dictionary_code", code).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if(dictionary == null){
            return sendResult400();
        }
        EntityWrapper<Dictionary> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("parent_id", dictionary.getId());
        entityWrapper.eq("logic_delete", Constants.DEL_LOGIC_NORMAL);
        entityWrapper.orderBy("dictionary.order",true);
        List<Dictionary> list = dictionary.selectList(entityWrapper);
        List<DictionaryDTO> dtoList = BeanMapper.mapList(list, DictionaryDTO.class);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, dtoList);
    }


    /**
     * 根据字典code获取配置的属性值
     * @param dictionaryCode 配置的字典值
     * @return
     */
    @RequestMapping(value = "/order-config-value", method = RequestMethod.GET)
    public BaseResult getOderConfigValue(@RequestParam String dictionaryCode) {
        //检查code是否存在，若存在返回提示
        MallConfig mallConfig = new MallConfig().selectOne(new EntityWrapper<MallConfig>().eq("dictionary_code", dictionaryCode)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (ValidationUtil.isEmpty(mallConfig)) {
            return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, mallConfig);
    }

    /**
     * 查询启用的物流公司
     *
     * @return
     */
    @GetMapping("/query")
    public BaseResult query() {
        List<LogisticsCompany> list = new LogisticsCompany().selectList(new EntityWrapper<LogisticsCompany>().setSqlSelect("id, name").eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("sort", false));
        return sendResult200(list);
    }

}
